import { createRouter, createWebHashHistory } from "vue-router"
import { ElMessage } from 'element-plus'
const routes = [
	// 默认首页
    { path: "/", redirect: "/dashboard" },
    {
        path: "/",
        name: "",
        // 设置鉴权
        meta: { permission: true },
        component: () => import("../pages/Home.vue"),
        // 嵌套路由
        children: [
            {
                path : "/dashboard",
                name : "dashboard",
                component : () => import("../pages/home/dashboard.vue")
            },
            {
                path: "/index",
                name: "index",
                meta: { permission: true },
                component: () => import("../pages/index/index.vue"),
                // 嵌套路由
                children: [
                    { path: "/", redirect: "/recommend" },
                    {
                        path: 'recommend',
                        component: () => import("../pages/index/recommend.vue")
                    },
                    {
                        path: 'hot',
                        component: () => import("../pages/index/hot.vue")
                    }
                ]
            },
            {
                path: "/movie",
                name: "movie",
                meta: { permission: true },
                component: () => import("../pages/movie/Movie.vue"),
                // 嵌套路由
                children: [
                    { path: "/", redirect: "/updata" },
                    {
                        path: 'add',
                        component: () => import("../pages/movie/addMovie.vue")
                    },
                    {
                        path: 'updata',
                        component: () => import("../pages/movie/updataMovie.vue")
                    }
                ]
            },
            {
                path: "/article",
                name: "article",
                meta: { permission: true },
                component: () => import("../pages/article/Article.vue"),
                // 嵌套路由
                children: [
                    { path: "/", redirect: "/updata" },
                    {
                        path: 'add',
                        component: () => import("../pages/article/addArticle.vue")
                    },
                    {
                        path: 'updata',
                        component: () => import("../pages/article/updataArticle.vue")
                    }
                ]
            },
            {
                path: "/user",
                name: "user",
                meta: { permission: true },
                component: () => import("../pages/user/User.vue"),
                // 嵌套路由
                children: [
                    { path: "/", redirect: "/userList" },
                    {
                        path: 'userList',
                        component: () => import("../pages/user/userList.vue")
                    }
                ]
            },
            {
                path: "/comment",
                name: "comment",
                meta: { permission: true },
                component: () => import("../pages/comment/Comment.vue"),
                // 嵌套路由
                children: [
                    { path: "/", redirect: "/commentList" },
                    {
                        path: 'commentList',
                        component: () => import("../pages/comment/commentList.vue")
                    },
                    {
                        path: 'checkCommentList',
                        component: () => import("../pages/comment/checkCommentList.vue")
                    }
                ]
            },
			{
                path: "/articleComments",
                name: "articleComments",
                meta: { permission: true },
                component: () => import("../pages/articleComments/articleComments.vue"),
                // 嵌套路由
                children: [
                    { path: "/", redirect: "/commentList" },
                    {
                        path: 'commentList',
                        component: () => import("../pages/articleComments/commentList.vue")
                    },
                    {
                        path: 'checkCommentList',
                        component: () => import("../pages/articleComments/checkCommentList.vue")
                    }
                ]
            }
        ]
    },
	// 单独的大屏页
	{
		path : "/aloneDashboard",
		name : "aloneDashboard",
		component : () => import("../pages/dashboard.vue")
	},
    {
        path: "/login",
        name: "login",
        // 单独的路由守卫
        beforeEnter: (to, from) => {
            if (sessionStorage.getItem("token")) {
                ElMessage.success("已登录~~~")
                return router.push("/")
            }
            return true
        },
        component: () => import("../pages/Login.vue")
    },

]
const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,

})
// 路由守卫 判断是否登录
router.beforeEach((to, from, next) => {
    if (to.meta.permission) {
        if (sessionStorage.getItem("token")) {
            next()
        } else {
            ElMessage.error('请先登录!!!')
            next("/login")
        }
    } else {
        next()
    }
})
export default router